<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="../static/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="../static/js/monitor.js"></script>
    <style type="text/css">
        html {
            height: 100%;
        }

        <!-- /*不配置html的height，显示不出body的背景图像*/
        -->
        body {
            background-size: 100% 100%;
        }

    </style>
    <script>
        function initWebSocket() { //初始化websocket

            var serverPath = '[[${path}]]';
            var url = "ws://" + serverPath + '/webSocket';
            this.websock = new WebSocket(url);
            this.websock.onmessage = this.websocketonmessage;
            this.websock.onopen = this.websocketonopen;
            this.websock.onerror = this.websocketonerror;
            this.websock.onclose = this.websocketclose;

        }

        function websocketonopen() { //连接建立之后执行send方法发送数据

            /* this.websocketsend(JSON.stringify(msg)); */
        }

        function websocketonerror() { //连接建立失败重连
            /* this.initWebSocket(); */
        }

        function websocketonmessage(e) { //数据接收

            let redata = e.data.split("*****currentCursor*******");
            document.getElementById("body").style.backgroundImage = 'url(data:image/jpg;base64,' + redata[1].replace(/[\r\n]/g, '') + ')';
            switch (Number(redata[0])) {
                case  -1: //很奇怪，不知道为什么，支持获取系统鼠标样式之后，只要按下键盘，就报错，所有当有问题的时候返回-1 不改变样式，保存之前样式

                    break;
                case  32513:
                    document.getElementsByTagName("html")[0].style.cursor = "text";
                    break;
                case  32649:
                    document.getElementsByTagName("html")[0].style.cursor = "pointer";
                    break;
                case  32643:
                    document.getElementsByTagName("html")[0].style.cursor = "sw-resize";
                    break;
                case  32644:
                    document.getElementsByTagName("html")[0].style.cursor = "w-resize";
                    break;
                case  32645:
                    document.getElementsByTagName("html")[0].style.cursor = "n-resize";
                    break;
                case  32642:
                    document.getElementsByTagName("html")[0].style.cursor = "se-resize";
                    break;
                default :
                    document.getElementsByTagName("html")[0].style.cursor = "default";
                    break;
            }
            //document.getElementById("img").setAttribute("src",'data:image/png;base64,'+redata[1].replace(/[\r\n]/g, ''));
        }

        function websocketsend(Data) { //数据发送
            this.websock.send(Data);
        }

        function websocketclose(e) { //关闭
            console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
        }


    </script>
</head>
<body id="body">
<script>
    function run() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', "http://localhost:8080/index/run", true);    // 也可以使用POST方式，根据接口
        xhr.responseType = "blob";  // 返回类型blob
        // 定义请求完成的处理函数，请求前也可以增加加载框/禁用下载按钮逻辑
        xhr.onload = function () {
            // 请求完成
            if (this.status === 200) {
                // 返回200
                var blob = this.response;
                var reader = new FileReader();
                reader.readAsDataURL(blob);  // 转换为base64，可以直接放入a表情href
                reader.onload = function (e) {
                    // 转换完成，创建一个a标签用于下载
                    // document.getElementById("img").setAttribute("src",'data:image/gif;'+e.target.result);
                    document.getElementById("body").style.backgroundImage = 'url(data:image/png;' + e.target.result + ')';
                }

            }
        };
        // 发送ajax请求
        xhr.send();
    }

    //setInterval('run()', 0);s

    initWebSocket();
    document.oncontextmenu = function(){//阻止上级事件
        return false;
    }
    window.document.onkeydown= function(){//阻止上级事件
        return false;
    }

    window.document.onkeyup= function(){//阻止上级事件
        return false;
    }
</script>
</body>